﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;

namespace SedionFormDesigner.Infrastructure
{
    /// <summary>
    ///  实现针对EventPropertyDescriptor调用的类型转化
    /// </summary>
    public class EventConverter : TypeConverter
    {
        private EventDescriptor eventDesc;

        internal EventConverter(EventDescriptor eventDesc)
        {
            this.eventDesc = eventDesc;
        }

        /// <summary>
        ///  判断能否由目标类型转化
        /// </summary>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(String))
                return true;
            return base.CanConvertFrom(context, sourceType);
        }

        /// <summary>
        ///  判断能否转化为目标类型
        /// </summary>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(String))
                return true;
            return base.CanConvertTo(context, destinationType);
        }

        /// <summary>
        ///  有目标类型转化为字符串类型
        /// </summary>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value == null)
                return null;

            if (value is String)
            {
                if (((String)value).Length == 0)
                    return null;
                return value;
            }

            return base.ConvertFrom(context, culture, value);
        }

        /// <summary>
        ///  转化为目标类型
        /// </summary>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(String))
                return value == null ? String.Empty : value;
            return base.ConvertTo(context, culture, value, destinationType);
        }

        /// <summary>
        ///  返回包含标准值的集合
        ///  如果不支持标准值则为null
        /// </summary>
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            String[] eventMethods = null;

            if (context != null)
            {
                IEventBindingService ebService = (IEventBindingService)context.GetService(typeof(IEventBindingService));
                if (ebService != null)
                {
                    ICollection methods = ebService.GetCompatibleMethods(eventDesc);
                    eventMethods = new String[methods.Count];
                    Int32 i = 0;
                    foreach (String s in methods)
                    {
                        eventMethods[i++] = s;
                    }
                }
            }

            return new StandardValuesCollection(eventMethods);
        }

        /// <summary>
        ///  如果是exclusive列表那么就没有其他有效值
        ///  如果不是那么会调用其他有效值
        /// </summary>
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }

        /// <summary>
        ///  如果支持标准值,那么可以从列表中提取
        /// </summary>
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}
